package com.ludaxu.zhbj;

import com.ludaxu.zhbj.utils.PrefUtils;

import android.R.id;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

public class SplashActivity extends Activity {
	RelativeLayout rlRoot;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		rlRoot = (RelativeLayout) findViewById(R.id.root);
		startAnim();
	}

	/**
	 * 开启动画
	 */
	private void startAnim() {
		// 动画集合 ,false为不共享插补器
		AnimationSet set = new AnimationSet(false);
		// 旋转动画
		RotateAnimation rotate = new RotateAnimation(0, 360,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		// 设置动画的旋转时间
		rotate.setDuration(3000);
		// 旋转后保持动画的状态,不返回原来的状态
		rotate.setFillAfter(true);
		// 缩放动画
		ScaleAnimation scale = new ScaleAnimation(0, 1, 0, 1,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		// 缩放动画的时长
		scale.setDuration(5000);
		// 缩放后保持动画的状态,不返回原来的状态
		scale.setFillAfter(true);
		// 渐变动画
		AlphaAnimation alpha = new AlphaAnimation(0, 1);
		// 渐变动画的时长
		alpha.setDuration(5000);
		// 渐变后保持动画的状态,不返回原来的状态
		alpha.setFillAfter(true);

		// 动画集合添加两个动画
		set.addAnimation(rotate);
		set.addAnimation(scale);
		set.addAnimation(alpha);
		// 设置动画监听,监听动画开始,结束,重复
		set.setAnimationListener(new AnimationListener() {
			// 动画开始
			@Override
			public void onAnimationStart(Animation animation) {

			}

			// 动画重复
			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			// 动画结束
			@Override
			public void onAnimationEnd(Animation animation) {
				// 闪屏页动画结束之后,跳转到引导页
				jumpNextPage();
			}
		});

		// 让rlRoot启动动画集合,动画集合时间长短不同时,以时间长的为准
		rlRoot.startAnimation(set);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.splash, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * 跳转到下一页面
	 */
	private void jumpNextPage() {
		// 判断之前有没有显示过新手引导页
		boolean userGuide = PrefUtils.getBoolean(SplashActivity.this,
				"is_user_guide_showed", false);
		if (!userGuide) {
			// 跳转到新手引导页
			startActivity(new Intent(SplashActivity.this, GuideActivity.class));

		} else {
			// 跳转到主页面
			startActivity(new Intent(SplashActivity.this, MainActivity.class));

		}
		finish();

	}
}
